const path = require("path");
const nodeExternals = require("webpack-node-externals");
const { merge } = require("webpack-merge");
const { baseConfig } = require("../webpack.base.config");

module.exports = {
  mode: "development",
  target: "node", // 打包后运行在Node端的代码,默认为 web(运行在浏览器端)
  entry: path.resolve(__dirname, "..", "..", "server.js"),
  output: {
    path: path.resolve(__dirname, "..", "..", "dist/server"),
    filename: "[name].js",
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: "babel-loader",
      },
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        use: "ts-loader",
      },
      {
        test: /\.(png|jfif|jpg|svg|webp|gif)$/i,
        type: "asset/resource",
      },
      {
        test: /\.(woff2?|eot|ttf|otf)$/i,
        type: "asset/inline",
      },
      {
        test: /\.(txt|conf)$/i,
        type: "asset/source",
      },
      {
        test: /\.(css|less)$/i,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              importLoaders: 1,
              modules: {
                auto: (resourcePath) => resourcePath.endsWith(".module.less"), // 匹配.module.less文件来进行css模块化。
                localIdentName: "[local]_[hash:base64:10]",
              },
              // modules: true,
            },
          },
          {
            loader: "less-loader",
            options: {
              lessOptions: {
                modifyVars: {
                  "@primary-color": "#405ce2",
                },
                javascriptEnabled: true,
              },
            },
          },
        ],
      },
    ],
  },
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "..", "..", "src"),
    },
    extensions: [".ts", ".tsx", ".js", ".jsx"],
  },
  /**
   * 排除Node依赖
   * 如果不排除 node 依赖,就会吧 node 相关的包也进行打包进去
   * 因为最后的文件是运行在node环境的,所以是不需要node依赖的,要不然生成的文件太大了
   */
  externals: [nodeExternals()],
};
